package main

import (
	"fmt"
	"github.com/panjf2000/ants/v2"
	"sync"
	"time"
)

func main() {
	go tradition()
	go withAnts()
	time.Sleep(1 * time.Second)
}

func processTask(i int, prefix string) {
	fmt.Printf("%s process task %d\n", prefix, i)
}

func tradition() {
	var wg sync.WaitGroup

	for i := 0; i < 100_000; i++ {
		wg.Add(1)
		go func(j int) {
			defer wg.Done()
			processTask(j, "tra") // // 可能引发资源耗尽
		}(i)
	}
}

// // 使用 ants + WaitGroup（限制并发数）
func withAnts() {
	pool, _ := ants.NewPool(100)
	defer pool.Release()
	var wg sync.WaitGroup
	for i := 100_000; i < 200_000; i++ {
		wg.Add(1)
		_ = pool.Submit(func() {
			defer wg.Done()
			processTask(i, "ants") // // 可能引发资源耗尽
		})
	}
	wg.Wait()
}
